package edu.gdin.ecommerce.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import edu.gdin.ecommerce.dao.RoleDAO;
import edu.gdin.ecommerce.model.Func;
import edu.gdin.ecommerce.model.Role;
import edu.gdin.ecommerce.model.User;
import edu.gdin.ecommerce.service.IRoleService;

@Service("roleService")
public class RoleService implements IRoleService {

	private RoleDAO roleDAO;

	public RoleDAO getRoleDAO() {
		return roleDAO;
	}

	@Resource
	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	
	
	
	/**
	 * 获取所有role
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Role> getAllRoles() {
		return (List<Role>) this.roleDAO.list(Role.class);
	}
	
	/**
	 * 查询角色名
	 * @param roleName
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Role> queryRole(String roleName) {
		roleName = roleName.trim();
		
		String hql = "from Role r where r.name like '%" + roleName + "%'";
		return (List<Role>) this.roleDAO.query(hql);
	}
	
	/**
	 * 判断是否存在同名角色
	 * @param role
	 * @return
	 */
	public boolean isExist(Role role) {
		return this.roleDAO.isExist(Role.class, "name", role.getName());
	}
	
	/**
	 * 添加角色
	 * @param role
	 */
	public void addRole(Role role) {
		this.roleDAO.insert(role);
	}
	
	/**
	 * 根据roleId获取role
	 * @param roleId
	 * @return
	 */
	public Role getRoleById(Integer roleId) {
		return (Role) this.roleDAO.findByPk(Role.class, roleId);
	}
	
	/**
	 * 根据roleId获取role，同时加载对应Funcs
	 * @param roleId
	 * @return
	 */
	public Role getRoleWithFuncById(Integer roleId) {
		Role role = (Role) this.roleDAO.findByPk(Role.class, roleId);
		System.out.println("role - " + role.getName() + "'s funcs's size:" + role.getFuncs().size());
		return role;
	}
	
	/**
	 * 修改角色
	 * @param role
	 */
	public void editRole(Role role) {
		Role r = (Role) this.roleDAO.findByPk(Role.class, role.getRoleId());
		r.setName(role.getName());
		r.setRoleDesc(role.getRoleDesc());
		
		this.roleDAO.update(r);
	}
	
	/**
	 * 删除角色
	 * @param role
	 */
	public void delRole(Role role) {
		Role r = (Role) this.roleDAO.findByPk(Role.class, role.getRoleId());
		
		System.out.println("Funcs'size:" + r.getFuncs().size());
		r.setFuncs(new ArrayList<Func>());
		this.roleDAO.update(r);
		this.roleDAO.delete(r);
	}
	
	/**
	 * 配置权限
	 * @param role
	 * @param selectedIds
	 */
	public void setFunc(Role role, String selectedIds) {
		Role r = (Role) this.roleDAO.findByPk(Role.class, role.getRoleId());
		System.out.println("role - " + r.getName() + "'s funcs's size:" + r.getFuncs().size());
		
		List<Func> funcs = new ArrayList<Func>();
		if(selectedIds.equals("")) {
			r.setFuncs(funcs);
		} else {
			String[] funcIds = selectedIds.split(",");
			for (int i = 0; i < funcIds.length; i++) {
				Func func = new Func();
				func.setFuncId(Integer.parseInt(funcIds[i]));
				funcs.add(func);
			}
			r.setFuncs(funcs);
		}
		this.roleDAO.update(r);
	}
	
}
